<?php
/**
 * 文章
 * User: cxm
 * Date: 2020/12/3
 * Time: 15:26
 */
namespace App\Http\Controllers\Admin\Article;

use App\Http\Controllers\Admin\Controller;
use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller {
    /**
     * 文章列表
     * URL POST:/admin/article/getLists
     * @param Request $request
     * @param string keyword 关键字
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function getLists(Request $request){
        $params = $request->only(['start_time','end_time','keyword','page','limit']);
        // 时间判断
        $start_time = isset($params['start_time']) ? $params['start_time'] : '';
        if (!empty($start_time)) {
            $params['where'][] = ['created_at','>=',$start_time];
        }
        $end_time = isset($params['end_time']) ? $params['end_time'] : '';
        if (!empty($end_time)) {
            $params['where'][] = ['created_at','<=',$end_time];
        }
        // 关键字
        $keyword = isset($params['keyword']) ? $params['keyword'] : '';
        if (!empty($keyword)) {
            $params['where'][] = ['title','like','%'.$keyword.'%'];
        }
        // 图片
        $params['with'][] = 'file:id,path';
        $data = Article::listByWhere($params);
        return $this->success('查询成功',$data);
    }

    /**
     * 添加、编辑处理
     * URL POST:/admin/article/store
     * @param Request $request
     * @param int id 文章ID
     * @param string title 标题
     * @param int image 图片
     * @param string content 内容
     * @param int sort 排序
     * @return mixed
     */
    public function store(Request $request){
        $params = $request->only(['id','title','image','content','sort']);
        $id = isset($params['id']) ?  $params['id'] : 0;
        $validator = Validator::make($params,[
            'title'         => ['required'],
            'image'         => ['required','integer'],
            'content'       => ['required']
        ],[
            'title.required'    => '标题不能为空',
            'image.required'    => '请上传图片',
            'image.integer'     => '请上传图片',
            'content.required'  => '请填写内容'
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        if ($id == 0) {
            $article = new Article();
        }else{
            $article = Article::find($id);
            if (!$article) {
                return $this->error('信息不存在');
            }
        }
        $article->title             = $params['title'];
        $article->file_id           = $params['image'];
        $article->content           = $params['content'];
        $article->sort              = isset($params['sort']) ? $params['sort'] : 1;
        if ($article->save()){
            return $this->success('操作成功');
        }
        return $this->error('失败');
    }

    /**
     * 删除
     * URL POST:/admin/article/delete
     * @param Request $request
     * @param array ids 文章ID
     * @return mixed
     */
    public function delete(Request $request){
        $params = $request->only(['ids']);
        if (!array_key_exists('ids',$params) || empty($params['ids'])) {
            return $this->error('缺少ID');
        }
        $result = Article::whereIn('id',$params['ids'])->delete();
        if (!$result) {
            return $this->error('操作失败');
        }
        return $this->success('操作成功');
    }

    /**
     * 修改排序
     * URL POST:/admin/article/updateSort
     * @param Request $request
     * @param int id
     * @param int sort 排序值
     * @return mixed
     */
    public function updateSort(Request $request){
        $params =$request->only(['id','sort']);
        if (!array_key_exists('id',$params) || empty($params['id'])) {
            return $this->error('缺少ID');
        }
        $sort = isset($params['sort']) ? $params['sort'] : 1;
        $detail = Article::find($params['id']);
        if (!$detail) {
            return $this->error('活动不存在');
        }
        $where = [];
        $where[] = ['id',$params['id']];
        $field = ['sort'=>$sort];
        $result = Article::modify($where,$field);
        if ($result === false) {
            return $this->error('修改失败');
        }
        return $this->success('修改成功');
    }

    /**
     * 是否显示
     * URL POST:/admin/Article/updateStatus
     * @param Request $request
     * @param int id 活动ID
     * @return mixed
     */
    public function updateStatus(Request $request){
        $params = $request->only(['id']);
        if (!array_key_exists('id',$params) || empty($params['id'])) {
            return $this->error('缺少文章ID');
        }
        $trader = Article::find($params['id']);
        if (!$trader) {
            return $this->error('文章不存在');
        }
        $status = $trader->status == 1 ? 2 : 1;
        $where = [];
        $where[] = ['id',$params['id']];
        $field = ['status'=>$status];
        $res = Article::modify($where,$field);
        if ($res === false) {
            return $this->error('修改失败');
        }
        return $this->success('修改成功');
    }
}